package com.yonyou.cyxdms.basedata.service.controller.code;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyxdms.basedata.client.domains.vo.CodeVO;
import com.yonyou.cyxdms.basedata.client.domains.vo.DictVO;
import com.yonyou.cyxdms.basedata.client.dto.code.CodeDTO;
import com.yonyou.cyxdms.basedata.client.dto.code.SearchCodeDTO;
import com.yonyou.cyxdms.basedata.service.service.code.CodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author maxingjian
 * @since 2018-12-12
 */
@Api(tags = "查询TC_CODE")
@RestController
@RequestMapping("/code")
public class CodeController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(CodeController.class);

    @Autowired
    private CodeService codeService;

    /**
     * 按条件TCCODE信息
     *
     * @param queryParam 条件
     * @return java.util.List<com.yonyou.cyxdms.basedata.service.service.code.CodeService ;>
     * @author maxingjian
     * @since 2018/12/10
     */
    @GetMapping(value = "/queryTcCodeByType")
    @ApiOperation(value = "通过4位type获取对应的值")
    public List<CodeVO> queryTcCodeByType(@RequestParam Map<String, Object> queryParam) {
        logger.debug("------------------" + "分割线" + "--------------------");
        return codeService.getTcCodeByType(queryParam);
    }

    /**
     * 按条件TCCODE信息
     *
     * @return java.util.List<com.yonyou.cyxdms.basedata.service.service.code.CodeService ;>
     * @author maxingjian
     * @since 2018/12/10
     */
    @GetMapping(value = "/queryTcCode")
    public List<CodeVO> queryTcCode(@RequestParam(value = "appId", required = false) String appId) {
        return codeService.getTcCode(appId);
    }

    /**
     * 根据codeName查询codeId
     *
     * @auhtor ysj
     * @date 2019/10/21
     * @param codeName
     * @return
     */
    @GetMapping(value = "/getTcCodeByName")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public CodeVO getTcCodeByName(@RequestParam(value = "codeName", required = false) String codeName,
                                  @RequestParam(value = "type", required = false) Integer type) {
        return codeService.getTcCodeByName(codeName,type);
    }

    /**
     * 根据appId查询所有tc_code(pc登录)
     *
     * @author     ：quankk
     * @date       ：2019/7/9 18:13
     * @description：
     */
    @GetMapping(value = "/allCode")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public List<DictVO> allCode() {
        return codeService.allCode();
    }

    /**
     * 根据appId查询全部可用字典
     *
     * @author lyz
     * @date 2021/3/8
     */
    @GetMapping(value = "/allCodeAvailable")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public List<DictVO> allCodeAvailable() {
        return codeService.allCodeAvailable();
    }


    /**
     * 进行数据修改
     *
     * @param codeDTO 需要保存的DTO
     * @return Map<String,Object> 返回的数据map
     * @author chengliang
     * @since 2019-09-27
     */
    @ApiOperation(value = "修改CODE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @PutMapping(value = "/update")
    public Map<String, Object> update(@RequestBody CodeDTO codeDTO) {
        logger.debug("codeDTO:{}", codeDTO);
        return codeService.updateCodePo(codeDTO);
    }

    @ApiOperation(value = "删除CODE")
    @GetMapping("/delete/{codeId}")
    public Integer delete(@PathVariable("codeId") Integer codeId){
        return codeService.deleteByCodeId(codeId);
    }

    /**
     * 进行数据新增
     *
     * @param codeDTO 需要保存的DTO
     * @return Map<String,Object> 返回的数据map
     * @author chengliang
     * @since 2019-09-27
     */
    @ApiOperation(value = "新增CODE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @PostMapping
    public Map<String, Object> insert(@RequestBody CodeDTO codeDTO) {
        logger.debug("codeDTO:{}", codeDTO);

        return codeService.insertCodePo(codeDTO);
    }

    /**
     * ID查CODE
     *
     * @param id
     * @return com.yonyou.cyxdms.basedata.client.domains.vo.CodeVO
     * @author tanjie
     * @since 2019/10/16
     */
    @GetMapping("queryById")
    public CodeVO queryById(@RequestParam("Id") Long id) {
        return this.codeService.queryById(id);
    }



   /**
    * 根据codeId查询
    *
    * @param codeId
    * @param appId
    * @return com.yonyou.cyxdms.basedata.client.domains.vo.CodeVO
    * @author dengqiang
    * @since 2019/11/1
    */
    @GetMapping("/queryByCodeId")
    public CodeVO queryByCodeId(@RequestParam("codeId") Long codeId,@RequestParam("appId") String appId) {
        return codeService.queryByCodeId(codeId,appId);
    }


    /**
     * 分页查询字典项
     *
     * @author zhangjinyin
     * @since 2020/11/26
     */
    @GetMapping("/queryList")
    public IPage<CodeDTO> queryList(SearchCodeDTO dto) {
        Page<CodeDTO> page = new Page(dto.getCurrentPage(), dto.getPageSize());
        page.setRecords(codeService.queryList(page,dto));
        return page;
    }

    /**
     * 获取全部类型
     *

     * @author zhangjinyin
     * @since 2020/11/26
     */
    @GetMapping("/getAllCodeType")
    public List<CodeDTO> getAllCodeType(@RequestParam(value = "name",required = false) String name) {
        return codeService.getAllCodeType(name);
    }

    /**
     * 根据id 获取 字典项
     *
     * @param codeId
     * @author zhangjinyin
     * @since 2020/11/26
     */
    @GetMapping("/getCodeById")
    public CodeDTO getCodeById(@RequestParam("codeId") Integer codeId) {
        return codeService.getCodeById(codeId);
    }

    /**
     * 新增修改字典项
     *
     * @author zhangjinyin
     * @since 2020/11/26
     */
    @PostMapping("/saveCode")
    public Boolean saveCode(@RequestBody CodeDTO codeDTO) {
        return codeService.saveCode(codeDTO);
    }

    /**
     * 删除缓存中的字典户据
     */
    @ApiOperation(value = "删除缓存中的字典户据")
    @GetMapping("/refreshCode/{appId}")
    public void refreshCode(@PathVariable String appId) {
        codeService.refreshCode(appId);
    }
}